package dev.android.yeqh.iask_sina.adapter;

import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import dev.android.yeqh.iask_sina.R;
import dev.android.yeqh.iask_sina.SearchResultActivity;

public class keyListAdapter extends BaseAdapter{
	Activity context = null;
	List<String> keywords = null;
	int layout = 0;
	OnClickListener clickListener = new OnClickListener() {
		
		public void onClick(View view) {
			String key = ((TextView)view).getText().toString();
			Intent intent = new Intent(context, SearchResultActivity.class);
    		intent.putExtra("keyword", key);
    		context.startActivity(intent);
		}
	};
	
	public keyListAdapter(Activity context, List<String> keywords, int layout) {
		this.context = context;
		this.layout = layout;
		this.keywords = keywords;
	}

	public int getCount() {
		return (int)Math.round(Math.ceil(keywords.size()/3.0));
	}

	public Object getItem(int position) {
		return keywords.get(position);
	}

	public long getItemId(int position) {
		return 0;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		View view = context.getLayoutInflater().inflate(layout, null);
		TextView key1 = (TextView)view.findViewById(R.id.txt_keyword1);
		TextView key2 = (TextView)view.findViewById(R.id.txt_keyword2);
		TextView key3 = (TextView)view.findViewById(R.id.txt_keyword3);
		
		if (position * 3 < keywords.size()) {
			key1.setText(keywords.get(position * 3));
			key1.setOnClickListener(clickListener);
		}
		if (position * 3 + 1 < keywords.size()) {
			key2.setText(keywords.get(position * 3 + 1));
			key2.setOnClickListener(clickListener);
		}
		if (position * 3 + 2 < keywords.size()) {
			key3.setText(keywords.get(position * 3 + 2));
			key3.setOnClickListener(clickListener);
		}
		
		return view;
	}

}
